//
//  TheVillageTableViewCell.m
//  community
//
//  Created by 小崔 on 2020/5/29.
//  Copyright © 2020 小崔. All rights reserved.
//

#import "TheVillageTableViewCell.h"
#import "TheVillageCollectionViewCell.h"
#import "TheVillageViewVM.h"
static NSString * const theVillageCollectionViewCell = @"TheVillageCollectionViewCell";

@interface TheVillageTableViewCell()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) TheVillageViewVM * thevillageVM;
@property (nonatomic,assign) BOOL              iconImageUpdata;
@property (nonatomic,assign) int              itmeCount;

@end

@implementation TheVillageTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initLayout];
//        self.backgroundColor = [UIColor cyanColor];
    }
    return self;
}


- (void)initLayout {
    
    [self addSubview:self.collectionView];
    [self addSubview:self.vImage];
    [self addSubview:self.titleLable];
    [self addSubview:self.line];
    WEAKSELF
    
    [self.vImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(25);
        make.top.equalTo(weakSelf.mas_top).offset(0);
        make.width.equalTo(@3);
        make.height.equalTo(@15);
    }];
    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
          make.left.equalTo(weakSelf.vImage.mas_right).offset(5);
          make.centerY.equalTo(weakSelf.vImage.mas_centerY);
          make.height.equalTo(@20);
      }];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
          make.top.equalTo(weakSelf.titleLable.mas_bottom).offset(10);
          make.left.equalTo(weakSelf.mas_left);
          make.right.equalTo(weakSelf.mas_right);
          make.height.equalTo(@1);
      }];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.line.mas_bottom).offset(5);
        make.left.equalTo(weakSelf.mas_left);
        make.right.equalTo(weakSelf.mas_right);
        make.bottom.equalTo(weakSelf.mas_bottom);
    }];
    
    self.thevillageVM = [TheVillageViewVM new];
    RAC(self,iconImageUpdata) = RACObserve(self.thevillageVM, iconImageUpdata);
    [self.thevillageVM exchange];
}

- (void)setIconImageUpdata:(BOOL)iconImageUpdata{
    if (iconImageUpdata) {
        NSLog(@"%lu",(unsigned long)self.thevillageVM.imageArray.count);
        CGFloat number = self.thevillageVM.imageArray.count / 4;
        self.cellRowNuber = (self.thevillageVM.imageArray.count % 4) == 0 ? number:(number+1);
        NSLog(@"%f",self.cellRowNuber);
        [self.collectionView reloadData];
    }
}

//- (void)setDataArray:(NSArray *)dataArray {
//    [self.collectionView reloadData];
//}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 15, 10, 15);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}


//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 5;
}

#pragma mark --UICollectionViewDelegate
//- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
//    return 1;
//}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.thevillageVM.imageArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TheVillageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:theVillageCollectionViewCell forIndexPath:indexPath];
    cell.model = [NSString stringWithFormat:@"%@",self.thevillageVM.imageArray[indexPath.row][@"image"]];
    cell.title = [NSString stringWithFormat:@"%@",self.thevillageVM.imageArray[indexPath.row][@"title"]];
    return cell;
}

/// 代理回传出去
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//    CYRecommendModel *model = self.dataArray[indexPath.row];
//    NSURL *url = [NSURL URLWithString:model.url];
//
//    if ([self.webSiteCellDelegate respondsToSelector:@selector(websiteCellTableView_didSelectCell:indexPath:)]) {
//        [self.webSiteCellDelegate websiteCellTableView_didSelectCell:url indexPath:indexPath];
//    }
}
- (UICollectionView *)collectionView{
    if (!_collectionView) {
           UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
           // 需要一行显示2个item
           NSUInteger count = 8;
           CGFloat margin = 15;
           CGFloat itemW = (SCREEN_WIDTH - (count) * margin) / 4;
           CGFloat itemH = itemW;
           flow.itemSize = CGSizeMake(itemW, itemH);
           flow.scrollDirection = UICollectionViewScrollDirectionVertical;
           self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flow];
           [_collectionView registerClass:[TheVillageCollectionViewCell class] forCellWithReuseIdentifier:theVillageCollectionViewCell];
           _collectionView.delegate = self;
           _collectionView.dataSource = self;
           _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

-(UIImageView *)vImage{
    if (!_vImage) {
        self.vImage = [[UIImageView alloc]init];
        _vImage.image = [UIImage imageNamed:@"vertical"];
    }
    return _vImage;
}
- (UILabel *)titleLable{
    if (!_titleLable) {
        self.titleLable = [[UILabel alloc]init];
        _titleLable.text = @"标题";
        _titleLable.font = SYSTEMFONT(18);
//        rgba(43, 50, 61, 1)
        _titleLable.textColor = RGBCOLOR(43, 50, 61);
    }
    return _titleLable;
}
- (UIView *)line{
    if (!_line) {
        self.line = [[UIView alloc]init];
        _line.backgroundColor = RGBACOLOR(222, 222, 222, 1);
    }
    return _line;
}



@end
